package com.punai.monthanalysis.bo;

import cn.hutool.core.collection.CollUtil;
import com.punai.common.utils.NumberUtil;
import com.punai.externalccb.bo.YdCbfeeiteminfoStandardBo;
import com.punai.monthanalysis.domain.ProYdcbfxClzlkzqkb;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ProYdcbfxClzlkzqkbBo extends ProYdcbfxClzlkzqkb {
    private Integer rowspan = 1;
    private Integer colspan = 1;
    private String name;//材料名称
    private List<ProYdcbfxClzlkzqkbBo> children = new ArrayList<>();

    public Integer getRowspan() {
        return rowspan;
    }

    public void setRowspan(Integer rowspan) {
        this.rowspan = rowspan;
    }

    public Integer getColspan() {
        return colspan;
    }

    public void setColspan(Integer colspan) {
        this.colspan = colspan;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<ProYdcbfxClzlkzqkbBo> getChildren() {
        return children;
    }

    public void setChildren(List<ProYdcbfxClzlkzqkbBo> children) {
        this.children = children;
    }

    public void clearColRow() {
        this.setRowspan(0);
        this.setColspan(0);
    }

    public static List<ProYdcbfxClzlkzqkbBo> convertStandard(List<YdCbfeeiteminfoStandardBo> list){
        List<ProYdcbfxClzlkzqkbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        for (YdCbfeeiteminfoStandardBo standard : list) {
            ProYdcbfxClzlkzqkbBo bo = new ProYdcbfxClzlkzqkbBo();
            bo.setId(standard.getId());
            bo.setProId(standard.getProId());
            bo.setQc(standard.getYdDate());//期次Id
            bo.setItemName(standard.getCategoryName()==null?"":standard.getCategoryName());//分类名称
            bo.setName(standard.getName());
            bo.setUnit(standard.getUnit());

            bo.setSlkzzlmbcb(standard.getTargetAmount());
            bo.setPrice(standard.getTargetNoTaxPrice());
            bo.setTotal(standard.getTargetNoTaxTotal());

            bo.setCurrmonthxhsl(standard.getActualCostAmout());
            bo.setCurrmonthxhje(standard.getActualCostNoTaxTotal());
            bo.setKlmonthxhsl(standard.getKlActualAmout());
            bo.setKlmonthxhje(standard.getKlActualCostNoTaxTotal());

            bo.setKljcsl(standard.getDifferenceOfCzsjAmount());
            bo.setKljcje(standard.getKlDifferenceOfCzsjTotal());
            result.add(bo);
        }
        Map<String, List<ProYdcbfxClzlkzqkbBo>> listMap = result.stream().filter(v -> v.getItemName() != null).collect(Collectors.groupingBy(ProYdcbfxClzlkzqkbBo::getItemName, Collectors.toList()));
        return  convert(listMap);
    }

    public static List<ProYdcbfxClzlkzqkbBo> convert(Map<String, List<ProYdcbfxClzlkzqkbBo>> listMap) {
        List<ProYdcbfxClzlkzqkbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(listMap)) {
            return result;
        }
        for (Map.Entry<String, List<ProYdcbfxClzlkzqkbBo>> listEntry : listMap.entrySet()) {
            List<ProYdcbfxClzlkzqkbBo> entryValues = listEntry.getValue();
            for (int i = 0; i < entryValues.size(); i++) {
                ProYdcbfxClzlkzqkbBo bo = entryValues.get(i);
                if (i == 0){
                    bo.setRowspan(entryValues.size());
                }else {
                    bo.setItemName(null);
                    bo.clearColRow();
                }
                result.add(bo);
            }
        }
        return result;
    }

}
